package com.shiyipicture.shiyipicturebackend.controller;

import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.COSObjectInputStream;
import com.qcloud.cos.utils.IOUtils;
import com.shiyipicture.shiyipicturebackend.annotation.AuthCheck;
import com.shiyipicture.shiyipicturebackend.common.BaseResponse;
import com.shiyipicture.shiyipicturebackend.common.ResultUtils;
import com.shiyipicture.shiyipicturebackend.constant.UserConstant;
import com.shiyipicture.shiyipicturebackend.exception.BusinessException;
import com.shiyipicture.shiyipicturebackend.exception.ErrorCode;
import com.shiyipicture.shiyipicturebackend.manager.CosManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

  @Resource
  private CosManager cosManager;


  @AuthCheck(mustRole = "admin")
  @PostMapping("/test/upload")
  public BaseResponse<String> testUploadFile(@RequestPart("file")MultipartFile multipartFile){
    //文件目录
    String filename = multipartFile.getOriginalFilename();
    String filePath = String.format("/test/%s", filename);
    File file = null;
    try {
      file = File.createTempFile(filePath, null);
      multipartFile.transferTo(file);
      cosManager.putObject(filePath,file);
      return ResultUtils.success(filePath);
    } catch (IOException e) {
     log.error("file upload error,filePath = {}",filePath);
     throw new BusinessException(ErrorCode.SYSTEM_ERROR,"文件上传失败");
    } finally {
      boolean delete = file.delete();
      if (!delete) {
        log.error("file delete error,filePath = {}",filePath);
      }
    }
  }

  /**
   * 测试文件下载
   *
   * @param filepath 文件路径
   * @param response 响应对象
   */
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @GetMapping("/test/download/")
  public void testDownloadFile(String filepath, HttpServletResponse response) throws IOException {
    COSObjectInputStream cosObjectInput = null;
    try {
      COSObject cosObject = cosManager.getObject(filepath);
      cosObjectInput = cosObject.getObjectContent();
      // 处理下载到的流
      byte[] bytes = IOUtils.toByteArray(cosObjectInput);
      // 设置响应头
      response.setContentType("application/octet-stream;charset=UTF-8");
      response.setHeader("Content-Disposition", "attachment; filename=" + filepath);
      // 写入响应
      response.getOutputStream().write(bytes);
      response.getOutputStream().flush();
    } catch (Exception e) {
      log.error("file download error, filepath = " + filepath, e);
      throw new BusinessException(ErrorCode.SYSTEM_ERROR, "下载失败");
    } finally {
      if (cosObjectInput != null) {
        cosObjectInput.close();
      }
    }
  }


}
